const {getReverseByUsername, newReverse, delReverse, getReverseListByCoachId, updateReverse} = require("../controller/reverse");
const {SuccessModel, ErrorModel} = require('../model/response')
var express = require('express');
var router = express.Router();

/**
 * 学员路由操作
 * AppointTrain.vue: /new, /update, /del/:id, /detail
 * ReverseQuery.vue: /detail
 */
// 获取学员预约详情
router.get('/detail', (req, res, next) => {
  const result = getReverseByUsername(req.session.username)
  return result.then(row => {
    if (row.identify) {
      res.json(new SuccessModel(row))
    } else {
      res.json(new ErrorModel())
    }
  })
})
// 添加学员预约
router.post('/new', (req, res, next) => {
  const result = newReverse(req.body)
  return result.then(val => {
    if (val) {
      res.json(new SuccessModel('预约练车时间成功'))
    } else {
      res.json(new ErrorModel('预约练车时间失败'))
    }
  })
})
// 删除学员预约
router.post('/del/:id', (req, res, next) => {
  const result = delReverse(req.params.id)
  return result.then(val => {
    if (val) {
      res.json(new SuccessModel('删除预约练车时间成功'))
    } else {
      res.json(new ErrorModel('删除练车时间失败'))
    }
  })
})

/**
 * 教练路由操作
 * Schedule.vue: /update, /del/:id
 */
// 更新学员预约
router.post('/update', (req, res, next) => {
  const result = updateReverse(req.body)
  return result.then(val => {
    if (val) {
      res.json(new SuccessModel('更新学员预约练车时间成功'))
    } else {
      res.json(new ErrorModel('更新学员练车时间失败'))
    }
  })
})

// 根据教练编号获取该教练带领学员的预约信息(未用到该路由)
router.get('/list/:coach_id', (req, res, next) => {
  const result = getReverseListByCoachId(req.params.coach_id)
  return result.then(rows => {
    res.json(new SuccessModel(rows))
  })
})

module.exports = router
